<template>
  <el-container class="home_box">
    <!-- 头部去区域 -->
    <el-header>
      <div class="home_login">
        <img src="../assets/images/heima.png" alt="" />
        <span>电商后台管理项目</span>
      </div>
      <el-button @click="exit">退出</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="200px">
        <!-- 侧边栏菜单区域 -->
        <div class="toggle-button" @click="toggleCollapse($event)">|||</div>
        <el-menu
          background-color="#313743"
          text-color="#fff"
          active-text-color="#359bff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <el-submenu
            :index="item.id.toString()"
            :key="item.id"
            v-for="item in menulist"
          >
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="['iconfont-r', iconsObj[item.id]]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单区域 -->
            <el-menu-item
              :index="'/' + item.path"
              :key="item.id"
              v-for="item in item.children"
              @click="savaNavState('/' + item.path)"
            >
              <!-- 图标 -->
              <i class="el-icon-menu"></i>
              <!-- 文本 -->
              <span slot="title">{{ item.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右边区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  methods: {
    exit() {
      window.sessionStorage.clear()
      this.$router.push({ path: 'login' })
    },
    async getMenuList() {
      const { data: menuList } = await this.$http.get('menus')
      if (menuList.meta.status !== 200) {
        this.$message.error(menuList.meta.msg)
      }
      this.$message.success(menuList.meta.msg)
      this.menulist = menuList.data
    },
    // 点击按钮切换菜单的折叠和展开
    toggleCollapse(e) {
      this.isCollapse = !this.isCollapse
      if (!this.isCollapse) {
        e.target.parentNode.style.width = '200px'
      } else {
        e.target.parentNode.style.width = ''
      }
    },
    // 保存链接的激活状态
    savaNavState(path) {
      window.sessionStorage.setItem('activePath', path)
      this.activePath = path
    }
  },
  created: async function () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  }
}
</script>

<style lang="less" scoped>
.home_box {
  height: 100%;
}

.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  color: #fff;
  font-size: 20px;
  background-color: #313743;
  .home_login {
    display: flex;
    align-items: center;
  }
}

.el-aside {
  background-color: #313743;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #e9edf1;
}

.iconfont-r {
  margin-right: 10px;
  margin-left: 3px;
}

.toggle-button {
  color: #fff;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  background-color: #4a5064;
  letter-spacing: 4px;
}
</style>
